<template>
	<view style="margin: 4px;padding: 8px">
		<view>订单信息格式：<span style="color: crimson;">品名+数量+单位+备注，</span>记录之间用逗号隔开或者换行分开，</view>
		<view>例如：<span style="color: crimson;">小白菜5斤嫩的,排骨6斤，</span></view>
		<view>如果想信息分割更准确，可以将品名数量单位备注彼此之间用空格隔开，</view>
		<view>例如：<span style="color: crimson;">小白菜 5 斤 嫩的，馒头 5 包，</span></view>
		<view>此外可以将<span style="color: crimson;">excel</span>的订单信息或微信下单语言转文字后复制粘贴到订单信息框中进行识别。</view>

    <view class="cu-form-group margin-top">
      <textarea maxlength="-1" v-model="billNames" placeholder="订单信息"></textarea>
    </view>

    <view style="padding-top: 8px">
      <button class="cu-btn bg-blue lg margin-right shadow"  :class="[shape==='round'?'round':'']"
              @tap="onAddToShopCar"><text space="emsp">{{loading ? "添加中...":" 添加到购物车清单 "}}</text>
      </button>

    </view>
<!--    <view style="padding-top: 8px">-->
<!--        <button class="cu-btn bg-blue lg margin-right shadow"  :class="[shape==='round'?'round':'']"-->
<!--                @tap="onCreateSalOrder"><text space="emsp">{{loading ? "添加中...":" 生成销售订单 "}}</text>-->
<!--        </button>-->
<!--    </view>-->

	<view>手写单识别：点击上传按钮上传手写单后点击手写单识别按钮，生成订单文字信息到输入框中，最后点接生成订单即可自动生成订单。</view>
	<view>文字表格单识别：点击上传按钮上传文字表格单后点击文字表格单识别按钮，即可自动生成订单。</view>


    <view class="title top64">上传图片</view>
    <tui-upload :serverUrl="'https://zkerp.com.cn:8889/erp/erpEx/salOrder/uploadPath'" :size="3"  :value="imgs" :header="header"
                @complete="result" @remove="imgremove" :delConfirm="true" :limit="1" :sourceType="['album']" width="150" height="150">
    </tui-upload>
    <view style="padding-top: 8px">
        <button class="cu-btn bg-blue lg margin-right shadow"  :class="[shape==='round'?'round':'']"
                @tap="aiHandBill(0)"><text space="emsp">手写图片识别</text>
        </button>
    </view>

    <view style="padding-top: 8px">
      <button class="cu-btn bg-blue lg margin-right shadow"  :class="[shape==='round'?'round':'']"
              @tap="aiFormBill"><text space="emsp">文字表格单图片识别</text>
      </button>
    </view>


	</view>
</template>

<script>
  import { CUSTOM_ID } from "@/common/util/constants"
  export default {
    data() {
			return {
        shape:'',//round 圆形
        loading: false,
        imgs: [],
        fileUrl: '',
        fileList:[],
        serverUrl: '',
        header: {},
        billNames:"",
			}
		},
		methods: {
      imgremove(e) {
        console.log(e, this.imgs)
      },
      onAddToShopCar(){
        let customId =uni.getStorageSync(CUSTOM_ID);
        this.$http.post("/erpEx/stkBillPush/aiInputToShopCar", {billNames:this.billNames,customerId:customId,buildType:1}).then(res=>{
          console.log("aiInput-res",res)
          if(res.data.success){
            this.$tip.success("订单生成成功。")
            this.$Router.push({name: "mycart"})
          }
        }).catch((err) => {
          this.$tip.error("快捷下单失败，请联系管理员。")
        }).finally(() => {

        });
      },
      onCreateSalOrder(){

        let customId =uni.getStorageSync(CUSTOM_ID);
        this.$http.post("/erpEx/stkBillPush/aiInput", {billNames:this.billNames,customerId:customId,buildType:1}).then(res=>{
			      console.log("aiInput-res",res)
            if(res.data.success){
				      this.$tip.success("订单生成成功。")
              this.$Router.push({name: "orderIndex"})
            }
        }).catch((err) => {
          this.$tip.error("快捷下单失败，请联系管理员。")
        }).finally(() => {

        });
      },
      result(e) {

        let ima = e.imgArr[0];
        const b = ima.indexOf("//") + 2;
        const c = ima.indexOf("/", b);
        this.fileUrl = ima;
        //this.fileList.push(this.fileUrl);


        if(e.status === 1){
          let imgsrc =e.imgsrc[0];
          console.log("imgsrc",imgsrc.replace("/profile//",''))
          this.fileList.push(imgsrc.replace("/profile//",''));
        }
      },
      aiHandBill (type) {
        console.log("fileList",this.fileList)
        if(this.fileList.length===0){
          this.$tip.toast("请点击上传功能上传手写订单图片！");
        }else{
          const that = this
          this.$http.post('/erpEx/stkBillPush/aiHandBill', {fileList:this.fileList[0],type:type}).then(res => {
            // 执行成功
            console.log("aiHandBill-res",res);
            if (res.data.success) {
              this.billNames=res.data.result
            }
          }).catch((err) => {
            this.$tip.toast("提交异常，请联系管理员!"+err,5);
          }).finally(() => {

          })
        }

      },
      aiFormBill () {
        console.log("pppp",this.fileList);
        if(this.fileList.length===0){
            this.$tip.toast("请点击上传功能上传文字表格订单图片！");
        }else {
            if(this.billNames===null||this.billNames===''||this.billNames===undefined){
                this.$tip.toast("客户和订单信息不能为空，请输入信息！");
            }
            else{
                const that = this
              let customId =uni.getStorageSync(CUSTOM_ID);
                this.$http.post('/erpEx/stkBillPush/aiFormInput', {fileList:this.fileList[0],customerId:customId}).then(res => {
                  if (res.success) {
                    this.$tip.toast(res.result);
                  }
                }).catch((err) => {
                  this.$tip.toast("提交异常，请联系管理员!"+err,5);
                }).finally(() => {

                })
          }
        }

      },
		}
	}
</script>

<style>
.cu-form-group{

  min-width: calc(4em + 15px);

}
</style>
